###################################
# Title: The ‘Tough-on-Crime’ Left: Race, Gender, and
######## Elections of Law-and-Order Democrats
# Authors: Isabel Laterzo-Tingley & Leah Christiani
# Date: May 2024

# File: Appendix 1 -- Descriptives + Response Quality
#####################################

#clear global environ
rm(list=ls())

#packages
library(tidyverse)
library(naniar)
library(cregg)
library(ggplot2)
#library(scales)

#working rd
setwd("/Users/IsabelLaterzo/Dropbox/Research Projects/Tough-on-Crime, Race, Gender Paper - L. Christiani and I. Laterzo/Data/Replication Archive")

#read in data
data <- readRDS("survey1_data.rds")



################################################################################
########################## DESCRIPTIVE STATISTICS ##############################
################################################################################

data_descrip <- data %>%
  select(-cand_gender, -cand_race, -cand_tax, -cand_pubsec, -cand_edu, -chosen) %>%
  unique()


######################## Partisanship ########################

#dem total and percent
table(data_descrip$dems)
table(data_descrip$dems)/nrow(data_descrip)

#rep total and percent
table(data_descrip$reps)
table(data_descrip$reps)/nrow(data_descrip)

# full count by partisanship (question 1) and percent
table(data_descrip$pid)
table(data_descrip$pid)/nrow(data_descrip)

# full count by partisanship (question 2) and percent
table(data_descrip$pidLean)
table(data_descrip$pidLean)/nrow(data_descrip)


######################## Gender ########################

table(data_descrip$female)/nrow(data_descrip)

######################## Race ########################

table(data_descrip$race_white)/nrow(data_descrip)

table(data_descrip$race_black)/nrow(data_descrip)

table(data_descrip$race_hisp)/nrow(data_descrip)

table(data_descrip$race_asian)/nrow(data_descrip)


######################## LGBTQ ########################

table(data_descrip$queer)/nrow(data_descrip)

######################## EDU ########################

table(data_descrip$edu)/nrow(data_descrip)


######################## AGE ########################

table(data_descrip$age)/nrow(data_descrip)






################################################################################
#################### THERMOMETER STRAIGHTLINER MODELS ##########################
################################################################################


# labels for straightliners
### straightliners on thermometer (people who answer all the same value)
data$straight <- ifelse(data$thermometer_1 == data$thermometer_2 &
                          data$thermometer_1 == data$thermometer_3 &
                          data$thermometer_1 == data$thermometer_4,
                        1, 0)

#total observations pre straightliners
data_total_prespeed <- data %>%
  select(id) %>%
  unique()

#total observations post straightliners
data_total_postpeed <- data %>%
  filter(straight == 0) %>%
  select(id) %>%
  unique()

#full data frame without straighliners
filter_data <- data %>%
  filter(straight == 0)

# Isolate Democrats/Democrat Leaners without straightliners
dem_data <- data %>% filter(dems == 1) %>%
  filter(straight == 0)

#respondents?
dem_total <- dem_data %>%
  select(id) %>%
  unique()

# Isolate Republicans/Republican Leaners
rep_data <- data %>% filter(reps == 1) %>%
  filter(straight == 0)

#respondents?
rep_total <- rep_data %>%
  select(id) %>%
  unique()


# Figure A1: Preferences for Crime Platforms by Partisanship 
########### (No Thermometer Straightliners)

##### BASELINE MODEL: Pooled Respondents
base_model <- cj(filter_data,
                 chosen ~ cand_pubsec,
                 id = ~id,
                 estimate = "mm",
                 alpha = .1)

dem_model <- cj(dem_data,
                chosen ~ cand_pubsec,
                id = ~id,
                estimate = "mm",
                alpha = .1)

rep_model <- cj(rep_data,
                chosen ~ cand_pubsec,
                id = ~id,
                estimate = "mm",
                alpha = .1)


### combining
base_model$sample <- "Pooled"
dem_model$sample <- "Democrats"
rep_model$sample <- "Republicans"
base_pooled <- rbind(base_model, dem_model, rep_model)
base_pooled$sample <- factor(base_pooled$sample, 
                             levels = c("Pooled", "Democrats",
                                        "Republicans"),
                             ordered = T)





fig1 <- ggplot(base_pooled, aes(x = level, y = estimate)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) + 
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.48, 0.5, 0.52)) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"))

fig1

#ggsave("app_fig1_straightliners_plot.png", fig1, width=8, height=3)



# Figure A2: Preferences for Crime Platforms by Partisanship & Candidate Race
########### (No Thermometer Straightliners)


base_interact_race <- cj(filter_data,
                         chosen ~ cand_pubsec,
                         id = ~id,
                         estimate = "mm",
                         by = ~ cand_race,
                         alpha = .1)

dem_interact_race <- cj(dem_data,
                        chosen ~ cand_pubsec,
                        id = ~id,
                        estimate = "mm",
                        by = ~ cand_race,
                        alpha = .1)

rep_interact_race <- cj(rep_data,
                        chosen ~ cand_pubsec,
                        id = ~id,
                        estimate = "mm",
                        by = ~ cand_race,
                        alpha = .1)


### combining
base_interact_race$sample <- "Pooled"
dem_interact_race$sample <- "Democrats"
rep_interact_race$sample <- "Republicans"
race_pooled <- rbind(base_interact_race, dem_interact_race,
                     rep_interact_race)
race_pooled$sample <- factor(race_pooled$sample, 
                             levels = c("Pooled", "Democrats",
                                        "Republicans"),
                             ordered = T)



# Figure 2 A - MMs for preferences based on candidate race

fig2a <- ggplot(race_pooled, aes(x = level, y = estimate, linetype = cand_race)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = BY),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.475, 0.5, 0.525)) +
  scale_linetype_discrete(name="Candidate Race",
                          labels =c("Black", "White")) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"))

fig2a


#ggsave("plots/app_fig2a_straightliners_plot.png", fig2a, width=8, height=3)




# differences in MMs
diff_base_interact_race <- cj(filter_data,
                              chosen ~ cand_pubsec,
                              id = ~id,
                              estimate = "mm_diff",
                              by = ~ cand_race,
                              alpha = .1)


diff_dem_interact_race <- cj(dem_data,
                             chosen ~ cand_pubsec,
                             id = ~id,
                             estimate = "mm_diff",
                             by = ~ cand_race,
                             alpha = .1)

diff_rep_interact_race <- cj(rep_data,
                             chosen ~ cand_pubsec,
                             id = ~id,
                             estimate = "mm_diff",
                             by = ~ cand_race,
                             alpha = .1)

### combining diff
diff_base_interact_race$sample <- "Pooled"
diff_dem_interact_race$sample <- "Democrats"
diff_rep_interact_race$sample <- "Republicans"
race_diff_total <- rbind(diff_base_interact_race, diff_dem_interact_race,
                         diff_rep_interact_race)
race_diff_total$sample <- factor(race_diff_total$sample, 
                                 levels = c("Pooled", "Democrats",
                                            "Republicans"),
                                 ordered = T)


# plot Figure 2b - the differences in marginal means

fig2b <- ggplot(race_diff_total, aes(x = level, y = estimate)) +
  geom_hline(yintercept = 0.0, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.3)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper),
                width = .1,
                alpha = 0.8,
                position=position_dodge(0.3),
                lwd = 0.7) +
  facet_grid(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(-0.04, 0.00, 0.04)) +
  labs(x = "Crime Policy",
       y = "Difference in Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14))

fig2b

#ggsave("plots/app_fig2b_straightliners_plot.png", fig2b, width=8, height=2)





# Figure A3: Preferences for Crime Platforms by Partisanship & Candidate Gender
########### (No Thermometer Straightliners)

base_interact_gen <- cj(filter_data,
                        chosen ~ cand_pubsec,
                        id = ~id,
                        estimate = "mm",
                        by = ~ cand_gender,
                        alpha = .1)

dem_interact_gen <- cj(dem_data,
                       chosen ~ cand_pubsec,
                       id = ~id,
                       estimate = "mm",
                       by = ~ cand_gender,
                       alpha = .1)

rep_interact_gen <- cj(rep_data,
                       chosen ~ cand_pubsec,
                       id = ~id,
                       estimate = "mm",
                       by = ~ cand_gender,
                       alpha = .1)


### combining
base_interact_gen$sample <- "Pooled"
dem_interact_gen$sample <- "Democrats"
rep_interact_gen$sample <- "Republicans"
gender_pooled <- rbind(base_interact_gen, dem_interact_gen,
                       rep_interact_gen)
gender_pooled$sample <- factor(gender_pooled$sample, 
                               levels = c("Pooled", "Democrats",
                                          "Republicans"),
                               ordered = T)



# Figure 2 A - MMs for preferences based on candidate race

fig3a <- ggplot(gender_pooled, aes(x = level, y = estimate, linetype = cand_gender)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = BY),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.475, 0.5, 0.525)) + 
  scale_linetype_discrete(name="Candidate Gender",
                          labels =c("Female", "Male")) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"))

fig3a

#ggsave("plots/app_fig3a_straightliners_plot.png", fig3a, width=8, height=3)




# differences in MMs
diff_base_interact_gen <- cj(filter_data,
                             chosen ~ cand_pubsec,
                             id = ~id,
                             estimate = "mm_diff",
                             by = ~ cand_gender,
                             alpha = .1)


diff_dem_interact_gen <- cj(dem_data,
                            chosen ~ cand_pubsec,
                            id = ~id,
                            estimate = "mm_diff",
                            by = ~ cand_gender,
                            alpha = .1)

diff_rep_interact_gen <- cj(rep_data,
                            chosen ~ cand_pubsec,
                            id = ~id,
                            estimate = "mm_diff",
                            by = ~ cand_gender,
                            alpha = .1)

### combining diff
diff_base_interact_gen$sample <- "Pooled"
diff_dem_interact_gen$sample <- "Democrats"
diff_rep_interact_gen$sample <- "Republicans"
gen_diff_total <- rbind(diff_base_interact_gen, diff_dem_interact_gen,
                        diff_rep_interact_gen)
gen_diff_total$sample <- factor(gen_diff_total$sample, 
                                levels = c("Pooled", "Democrats",
                                           "Republicans"),
                                ordered = T)


# plot Figure 3b - the differences in marginal means

fig3b <- ggplot(gen_diff_total, aes(x = level, y = estimate)) +
  geom_hline(yintercept = 0.0, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.3)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper),
                width = .1,
                alpha = 0.8,
                position=position_dodge(0.3),
                lwd = 0.7) +
  facet_grid(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(-0.04, 0.00, 0.04)) +
  labs(x = "Crime Policy",
       y = "Difference in Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14))

fig3b

#ggsave("plots/app_fig3b_straightliners_plot.png", fig3b, width=8, height=2)




# Figure A4: Democrats' Preferences for Crime Platforms by Respondent Partisanship
########### & Candidate Race + Gender (No Thermometer Straightliners)

## subset by candidate Race, then examine gender

black_dem_cands <- dem_data %>% filter(cand_race == "Black")
white_dem_cands <- dem_data %>% filter(cand_race == "White")

dem_interact_genblack <- cj(black_dem_cands,
                            chosen ~ cand_pubsec,
                            id = ~id,
                            estimate = "mm",
                            by = ~ cand_gender,
                            alpha = .1)

dem_interact_genwhite <- cj(white_dem_cands,
                            chosen ~ cand_pubsec,
                            id = ~id,
                            estimate = "mm",
                            by = ~ cand_gender,
                            alpha = .1)

### combining
dem_interact_genblack$sample <- "Candidate: Black"
dem_interact_genwhite$sample <- "Candidate: White"
dem_interact_pooled <- rbind(dem_interact_genblack, dem_interact_genwhite)
dem_interact_pooled$sample <- factor(dem_interact_pooled$sample)



# Figure 4 a - second option

fig4a2 <- ggplot(dem_interact_pooled, aes(x = level, y = estimate, linetype = cand_gender)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.4)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = cand_gender),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.4),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.45, 0.5, 0.55)) +
  scale_linetype_discrete(name="Candidate Gender",
                          labels =c("Female", "Male")) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"))

fig4a2


#ggsave("plots/app_fig4a2_straightliners_plot.png", fig4a2, width=8, height=3)



# difference in marginal means

diff_dem_interact_genblack <- cj(black_dem_cands,
                                 chosen ~ cand_pubsec,
                                 id = ~id,
                                 estimate = "mm_diff",
                                 by = ~ cand_gender,
                                 alpha = .1)

diff_dem_interact_genwhite <- cj(white_dem_cands,
                                 chosen ~ cand_pubsec,
                                 id = ~id,
                                 estimate = "mm_diff",
                                 by = ~ cand_gender,
                                 alpha = .1)

### combining
diff_dem_interact_genblack$sample <- "Candidate: Black"
diff_dem_interact_genwhite$sample <- "Candidate: White"
diff_dem_interact_total <- rbind(diff_dem_interact_genblack,
                                 diff_dem_interact_genwhite)
diff_dem_interact_total$sample <- factor(diff_dem_interact_total$sample)



fig4b2 <- ggplot(diff_dem_interact_total, aes(x = level, y = estimate)) +
  geom_hline(yintercept = 0.0, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.3)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper),
                width = .1,
                alpha = 0.8,
                position=position_dodge(0.3),
                lwd = 0.7) +
  facet_grid(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(-0.04, 0.00, 0.04)) +
  labs(x = "Crime Policy",
       y = "Difference in Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14))

fig4b2

#ggsave("plots/app_fig4b2_straightliners_plot.png", fig4b2, width=8, height=2)



################################################################################
###################### CONJOINT STRAIGHTLINER MODELS ###########################
################################################################################


#total observations post conjoint straightliners
data_total_poststraight2 <- data %>%
  filter(all_candA == 0) %>%
  filter(all_candB == 0) %>%
  select(id) %>%
  unique()

#full data frame
filter_data2 <- data %>%
  filter(all_candA == 0) %>%
  filter(all_candB == 0)

# Isolate Democrats/Democrat Leaners without CJ straightliners
dem_data2 <- data %>% filter(dems == 1) %>%
  filter(all_candA == 0) %>%
  filter(all_candB == 0)

#respondents?
dem_total2 <- dem_data2 %>%
  select(id) %>%
  unique()

# Isolate Republicans/Republican Leaners
rep_data2 <- data %>% filter(reps == 1) %>%
  filter(all_candA == 0) %>%
  filter(all_candB == 0)

#respondents?
rep_total2 <- rep_data2 %>%
  select(id) %>%
  unique()



# Figure A5: Preferences for Crime Platforms by Partisanship (No Conjoint Straightliners)


base_model2 <- cj(filter_data2,
                  chosen ~ cand_pubsec,
                  id = ~id,
                  estimate = "mm",
                  alpha = .1)

dem_model2 <- cj(dem_data2,
                 chosen ~ cand_pubsec,
                 id = ~id,
                 estimate = "mm",
                 alpha = .1)

rep_model2 <- cj(rep_data2,
                 chosen ~ cand_pubsec,
                 id = ~id,
                 estimate = "mm",
                 alpha = .1)


### combining
base_model2$sample <- "Pooled"
dem_model2$sample <- "Democrats"
rep_model2$sample <- "Republicans"
base_pooled2 <- rbind(base_model2, dem_model2, rep_model2)
base_pooled2$sample <- factor(base_pooled2$sample, 
                              levels = c("Pooled", "Democrats",
                                         "Republicans"),
                              ordered = T)



fig5_cjstraight <- ggplot(base_pooled2, aes(x = level, y = estimate)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) + 
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.475, 0.5, 0.525)) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"))

fig5_cjstraight

#ggsave("plots/app_fig1_cjstraight_plot.png", fig1_cjstraight, width=8, height=3)



# Figure A6: Preferences for Crime Platforms by Respondent Partisanship & Candidate
########### Race (No Conjoint Straightliners)


base_interact_race2 <- cj(filter_data2,
                          chosen ~ cand_pubsec,
                          id = ~id,
                          estimate = "mm",
                          by = ~ cand_race,
                          alpha = .1)

dem_interact_race2 <- cj(dem_data2,
                         chosen ~ cand_pubsec,
                         id = ~id,
                         estimate = "mm",
                         by = ~ cand_race,
                         alpha = .1)

rep_interact_race2 <- cj(rep_data2,
                         chosen ~ cand_pubsec,
                         id = ~id,
                         estimate = "mm",
                         by = ~ cand_race,
                         alpha = .1)


### combining
base_interact_race2$sample <- "Pooled"
dem_interact_race2$sample <- "Democrats"
rep_interact_race2$sample <- "Republicans"
race_pooled2 <- rbind(base_interact_race2, dem_interact_race2,
                      rep_interact_race2)
race_pooled2$sample <- factor(race_pooled2$sample, 
                              levels = c("Pooled", "Democrats",
                                         "Republicans"),
                              ordered = T)



# Figure 2 A - MMs for preferences based on candidate race

fig6a_cjstraight <- ggplot(race_pooled2, aes(x = level, y = estimate, linetype = cand_race)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = BY),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.475, 0.5, 0.525)) +
  scale_linetype_discrete(name="Candidate Race",
                          labels =c("Black", "White")) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"))

fig6a_cjstraight


#ggsave("plots/app_fig6a_cjstraight_plot.png", fig2a_cjstraight, width=8, height=3)



# differences in MMs
diff_base_interact_race2 <- cj(filter_data2,
                               chosen ~ cand_pubsec,
                               id = ~id,
                               estimate = "mm_diff",
                               by = ~ cand_race,
                               alpha = .1)


diff_dem_interact_race2 <- cj(dem_data2,
                              chosen ~ cand_pubsec,
                              id = ~id,
                              estimate = "mm_diff",
                              by = ~ cand_race,
                              alpha = .1)

diff_rep_interact_race2 <- cj(rep_data2,
                              chosen ~ cand_pubsec,
                              id = ~id,
                              estimate = "mm_diff",
                              by = ~ cand_race,
                              alpha = .1)

### combining diff
diff_base_interact_race2$sample <- "Pooled"
diff_dem_interact_race2$sample <- "Democrats"
diff_rep_interact_race2$sample <- "Republicans"
race_diff_total2 <- rbind(diff_base_interact_race2, diff_dem_interact_race2,
                          diff_rep_interact_race2)
race_diff_total2$sample <- factor(race_diff_total2$sample, 
                                  levels = c("Pooled", "Democrats",
                                             "Republicans"),
                                  ordered = T)


# plot Figure 6b - the differences in marginal means

fig6b_cjstraight <- ggplot(race_diff_total2, aes(x = level, y = estimate)) +
  geom_hline(yintercept = 0.0, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.3)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper),
                width = .1,
                alpha = 0.8,
                position=position_dodge(0.3),
                lwd = 0.7) +
  facet_grid(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(-0.04, 0.00, 0.04)) +
  labs(x = "Crime Policy",
       y = "Difference in Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14))

fig6b_cjstraight

#ggsave("plots/app_fig6b_cjstraight_plot.png", fig2b_cjstraight, width=8, height=2)



# Figure A7: Preferences for Crime Platforms by Respondent Partisanship and Candidate
########### Gender (No Conjoint Straightliners)


base_interact_gen2 <- cj(filter_data2,
                         chosen ~ cand_pubsec,
                         id = ~id,
                         estimate = "mm",
                         by = ~ cand_gender,
                         alpha = .1)

dem_interact_gen2 <- cj(dem_data2,
                        chosen ~ cand_pubsec,
                        id = ~id,
                        estimate = "mm",
                        by = ~ cand_gender,
                        alpha = .1)

rep_interact_gen2 <- cj(rep_data2,
                        chosen ~ cand_pubsec,
                        id = ~id,
                        estimate = "mm",
                        by = ~ cand_gender,
                        alpha = .1)


### combining
base_interact_gen2$sample <- "Pooled"
dem_interact_gen2$sample <- "Democrats"
rep_interact_gen2$sample <- "Republicans"
gender_pooled2 <- rbind(base_interact_gen2, dem_interact_gen2,
                        rep_interact_gen2)
gender_pooled2$sample <- factor(gender_pooled2$sample, 
                                levels = c("Pooled", "Democrats",
                                           "Republicans"),
                                ordered = T)



# Figure 7 A - MMs for preferences based on candidate race

fig7a_cjstraight <- ggplot(gender_pooled2, aes(x = level, y = estimate, linetype = cand_gender)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = BY),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.475, 0.5, 0.525)) + 
  scale_linetype_discrete(name="Candidate Gender",
                          labels =c("Female", "Male")) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"))

fig7a_cjstraight

#ggsave("plots/app_fig7a_cjstraight_plot.png", fig3a_cjstraight, width=8, height=3)




# differences in MMs
diff_base_interact_gen2 <- cj(filter_data2,
                              chosen ~ cand_pubsec,
                              id = ~id,
                              estimate = "mm_diff",
                              by = ~ cand_gender,
                              alpha = .1)


diff_dem_interact_gen2 <- cj(dem_data2,
                             chosen ~ cand_pubsec,
                             id = ~id,
                             estimate = "mm_diff",
                             by = ~ cand_gender,
                             alpha = .1)

diff_rep_interact_gen2 <- cj(rep_data2,
                             chosen ~ cand_pubsec,
                             id = ~id,
                             estimate = "mm_diff",
                             by = ~ cand_gender,
                             alpha = .1)

### combining diff
diff_base_interact_gen2$sample <- "Pooled"
diff_dem_interact_gen2$sample <- "Democrats"
diff_rep_interact_gen2$sample <- "Republicans"
gen_diff_total2 <- rbind(diff_base_interact_gen2, diff_dem_interact_gen2,
                         diff_rep_interact_gen2)
gen_diff_total2$sample <- factor(gen_diff_total2$sample, 
                                 levels = c("Pooled", "Democrats",
                                            "Republicans"),
                                 ordered = T)


# plot Figure 7b - the differences in marginal means

fig7b_cjstraight <- ggplot(gen_diff_total2, aes(x = level, y = estimate)) +
  geom_hline(yintercept = 0.0, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.3)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper),
                width = .1,
                alpha = 0.8,
                position=position_dodge(0.3),
                lwd = 0.7) +
  facet_grid(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(-0.04, 0.00, 0.04)) +
  labs(x = "Crime Policy",
       y = "Difference in Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14))

fig7b_cjstraight

#ggsave("plots/app_fig7b_cjstraight_plot.png", fig3b_cjstraight, width=8, height=2)


# Figure A8: Democrats' Preferences for Crime Platforms by Respondent Partisanship
########### & Candidate Gender (No Conjoint Straightliners)

## subset by candidate Race, then examine gender

black_dem_cands2 <- dem_data2 %>% filter(cand_race == "Black")
white_dem_cands2 <- dem_data2 %>% filter(cand_race == "White")

dem_interact_genblack2 <- cj(black_dem_cands2,
                             chosen ~ cand_pubsec,
                             id = ~id,
                             estimate = "mm",
                             by = ~ cand_gender,
                             alpha = .1)

dem_interact_genwhite2 <- cj(white_dem_cands2,
                             chosen ~ cand_pubsec,
                             id = ~id,
                             estimate = "mm",
                             by = ~ cand_gender,
                             alpha = .1)

### combining
dem_interact_genblack2$sample <- "Candidate: Black"
dem_interact_genwhite2$sample <- "Candidate: White"
dem_interact_pooled2 <- rbind(dem_interact_genblack2, dem_interact_genwhite2)
dem_interact_pooled2$sample <- factor(dem_interact_pooled2$sample)



# Figure 8 a - second option

fig482_cjstraight <- ggplot(dem_interact_pooled2, aes(x = level, y = estimate, linetype = cand_gender)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.4)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = cand_gender),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.4),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.45, 0.5, 0.55)) +
  scale_linetype_discrete(name="Candidate Gender",
                          labels =c("Female", "Male")) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"))

fig482_cjstraight


#ggsave("plots/app_fig482_cjstraight_plot.png", fig4a2_cjstraight, width=8, height=3)



# difference in marginal means

diff_dem_interact_genblack2 <- cj(black_dem_cands2,
                                  chosen ~ cand_pubsec,
                                  id = ~id,
                                  estimate = "mm_diff",
                                  by = ~ cand_gender,
                                  alpha = .1)

diff_dem_interact_genwhite2 <- cj(white_dem_cands2,
                                  chosen ~ cand_pubsec,
                                  id = ~id,
                                  estimate = "mm_diff",
                                  by = ~ cand_gender,
                                  alpha = .1)

### combining
diff_dem_interact_genblack2$sample <- "Candidate: Black"
diff_dem_interact_genwhite2$sample <- "Candidate: White"
diff_dem_interact_total2 <- rbind(diff_dem_interact_genblack2,
                                  diff_dem_interact_genwhite2)
diff_dem_interact_total2$sample <- factor(diff_dem_interact_total2$sample)



fig8b2_cjstraight <- ggplot(diff_dem_interact_total2, aes(x = level, y = estimate)) +
  geom_hline(yintercept = 0.0, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.3)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper),
                width = .1,
                alpha = 0.8,
                position=position_dodge(0.3),
                lwd = 0.7) +
  facet_grid(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(-0.04, 0.00, 0.04)) +
  labs(x = "Crime Policy",
       y = "Difference in Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14))

fig8b2_cjstraight

#ggsave("plots/app_fig8b2_cjstraight_plot.png", fig4b2_cjstraight, width=8, height=2)





